﻿# -*- coding: utf-8 -*-
import sys
from reader import *
import jpype
import os
from jpype import java
from jpype import *
import sys
def main(*args):
	data =''
	if(len(args)!=3 or (args[1]!="-u" and args[1]!="-f")):
		return -1
	
	op = args[1]
	param = args[2]
	
	if(op=="-f"):
		data = unicode(open(param, 'r').read(), errors='ignore')
	else:
		data = Reader(param).getText()


	inicio=0
	cant = 300
	sup=inicio+cant
	input = data[inicio:sup]
	NewsClassifier=jpype.JClass('classifier.NewsClassifier')
	String = jpype.JClass('java.lang.String')
	newsClassifier = NewsClassifier.createFromModel(String("modelo.model"))
	categoryAnt=""
	inicio=sup
	sup=inicio+cant
	list=[]
	count=0
	while(len(data)>sup+cant):
		input = data[inicio:sup]
		sup=sup+cant
		category = newsClassifier.classifyMessage(String(input))
		list.append(category)
		if(category!=categoryAnt):
			count=1
			categoryAnt=category
		else:
			count=count+1
		if(count==3):
			break;
		'''if(categoryAnt!=category):
			break;
		categoryAnt=category
		list.append(category)'''
	print categoryAnt
	print list
	'''for x in range(1,cant+1):
		input = data[init:sup+countSet/cant]
		
		NewsClassifier=jpype.JClass('classifier.NewsClassifier')
		String = jpype.JClass('java.lang.String')
		newsClassifier = NewsClassifier.createFromModel(String("modelo.model"))
		category = newsClassifier.classifyMessage(String(input))
		d[unicode(category)] = d[unicode(category)]+(len(data)-sup)
		init=sup
		sup=sup+countSet/cant+1
	max=-1
	category2=''
	for x in d:
		if d[x]>max:
			max=d[x]
			category2=x
	print d
	print category2

	NewsClassifier=jpype.JClass('classifier.NewsClassifier')
	String = jpype.JClass('java.lang.String')
	newsClassifier = NewsClassifier.createFromModel(String("modelo.model"))
	category = newsClassifier.classifyMessage(String(data))
	print category
	print data
	NewsClassifier=jpype.JClass('classifier.NewsClassifier')
	String = jpype.JClass('java.lang.String')
	newsClassifier = NewsClassifier.createFromModel(String("modelo.model"))
	category = newsClassifier.classifyMessage(String(data))
	print category'''
	return 0

if __name__ == '__main__':
	_jvmArgs = ["-ea"] # enable assertions
	_jvmArgs.append("-Djava.class.path="+os.environ["CLASSPATH"])
	jpype.startJVM(jpype.getDefaultJVMPath(), *_jvmArgs)
	sys.exit(main(*sys.argv))


    